package com.example.kotlindemo.net;


import com.example.kotlindemo.net.gsonnull.DoubleDefault0Adapter;
import com.example.kotlindemo.net.gsonnull.IntegerDefault0Adapter;
import com.example.kotlindemo.net.gsonnull.LongDefault0Adapter;
import com.example.kotlindemo.net.gsonnull.NullStringToEmptyAdapterFactory;
import com.example.kotlindemo.net.okhttp.HttpLoggingInterceptor;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * @功能:
 * @Creat 2019/10/23 8:54
 * @User Lmy
 * @Compony zaituvideo
 */
public class ApiHelperl {
    private static volatile ApiHelperl mInstance;
    private OkHttpClient client;
    private Retrofit mRetrofit;
    private HttpLoggingInterceptor loggingInterceptor;
    private String baseUrl = "http://10.10.10.116:8060/";//测试服务器的前缀
    public static String urlTitle = "http://upload.miguanli.com/";//测试服务器的前缀

    /**
     * 单例封装
     *
     * @return
     */
    public static ApiHelperl getInstance() {
        if (mInstance == null) {
            mInstance = new ApiHelperl();
        }
        return mInstance;
    }

    public ApiHelperl() {
        loggingInterceptor = new HttpLoggingInterceptor();
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
    }

    /**
     * 初始化Retrofit
     */
    public ApiHelperl buildRetrofit() {
        Gson gson = new GsonBuilder().
                registerTypeAdapterFactory(new NullStringToEmptyAdapterFactory())
                .registerTypeAdapter(int.class, new IntegerDefault0Adapter())
                .registerTypeAdapter(Integer.class, new IntegerDefault0Adapter())
                .registerTypeAdapter(Double.class, new DoubleDefault0Adapter())
                .registerTypeAdapter(double.class, new DoubleDefault0Adapter())
                .registerTypeAdapter(Long.class, new LongDefault0Adapter())
                .registerTypeAdapter(long.class, new LongDefault0Adapter())
                .create();

        okhttp3.logging.HttpLoggingInterceptor loggingInterceptor = new okhttp3.logging.HttpLoggingInterceptor(new okhttp3.logging.HttpLoggingInterceptor.Logger() {
            @Override
            public void log(String message) {
                LogUtil.i("HttpLoggingInterceptor -->" + message);
            }
        });
        loggingInterceptor.setLevel(okhttp3.logging.HttpLoggingInterceptor.Level.BODY);
        client = new OkHttpClient.Builder()//okhttp设置部分，此处还可再设置网络参数
                .connectTimeout(30, TimeUnit.SECONDS)
                .readTimeout(30, TimeUnit.SECONDS)
                .writeTimeout(30, TimeUnit.SECONDS)
                .addInterceptor(loggingInterceptor)
                .build();
        mRetrofit = new Retrofit.Builder()
                .client(client)
                // 设置请求的域名
                .baseUrl(baseUrl)
                // 设置解析转换工厂
                .addConverterFactory(GsonConverterFactory.create(gson))
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();
        return this;
    }


    /**
     * 初始化Retrofit
     */
    public ApiHelperl buildRetrofit(String baseUrl) {
        Gson gson = new GsonBuilder().
                registerTypeAdapterFactory(new NullStringToEmptyAdapterFactory())
                .registerTypeAdapter(int.class, new IntegerDefault0Adapter())
                .registerTypeAdapter(Integer.class, new IntegerDefault0Adapter())
                .registerTypeAdapter(Double.class, new DoubleDefault0Adapter())
                .registerTypeAdapter(double.class, new DoubleDefault0Adapter())
                .registerTypeAdapter(Long.class, new LongDefault0Adapter())
                .registerTypeAdapter(long.class, new LongDefault0Adapter())
                .create();
        client = new OkHttpClient.Builder()//okhttp设置部分，此处还可再设置网络参数
                .connectTimeout(30, TimeUnit.SECONDS)
                .readTimeout(30, TimeUnit.SECONDS)
                .writeTimeout(30, TimeUnit.SECONDS)
                .addInterceptor(loggingInterceptor)
                .build();
        mRetrofit = new Retrofit.Builder()
                .client(client)
                // 设置请求的域名
                .baseUrl(baseUrl)
                // 设置解析转换工厂
                .addConverterFactory(GsonConverterFactory.create(gson))
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();
        return this;
    }


    /**
     * 初始化Retrofit
     */
    public ApiHelperl buildDownloadRetrofit() {
        client = new OkHttpClient.Builder()//okhttp设置部分，此处还可再设置网络参数
                .build();
        mRetrofit = new Retrofit.Builder()
                .client(client)
                .baseUrl(baseUrl)
                // 设置解析转换工厂
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();
        return this;
    }

    public <T> T createService(Class<T> serviceClass) {
        return mRetrofit.create(serviceClass);
    }


}


